/******************************************************
 *                                                     
 *              Score System               
 *                                                     
 *******************************************************/

/**
 * LoginServlet.java
 *
 * 网页的响应Servlet
 *
 * @author sundy
 * @version 0.0.1
 * lastModified 2011-01-01
 */

package com.sundy.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sundy.core.Context;
import com.sundy.core.Utility;
import com.sundy.tools.InitSystem;

public class LoginServlet extends AbstractServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9188465390449456603L;

	public LoginServlet() {
	}

	protected void doGet(Context context, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Utility.showError(request, response, "不支持该方法！");
	}

	protected void doPost(Context context, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String userName = request.getParameter("name");
		String password = request.getParameter("password");
		String createAdmin = request.getParameter("newAdmin");
		if (userName == null || userName.equals("") || password == null
				|| password.equals("")) {
			Utility.showError(request, response, "参数错误");
			return;
		}
		if (createAdmin != null && createAdmin.equals("1")
				&& InitSystem.needCreateAdmin()) {
			String[] args = new String[2];
			args[0] = userName;
			args[1] = password;
			InitSystem.main(args);
		}
		context.login(userName, password);
		response.sendRedirect(request.getContextPath());
	}
}
